<div class="doc-item" id="doc-1396"><h2 class="doc-item-name">template<span class="namespace">(clojure.contrib.template)</span></h2><p class="macro">Macro</p><ul class="var-arglists"><li>(<span class="var-name">template</span> &amp; form)</li></ul><div class="var-docstring"><p>Expands to a fn using _1, _2, _3, etc. as arguments (_ is the same
  as _1).  Any sub-expressions without any _* variables are evaluated
  when the fn is created, not when it is called.</p></div><div class="var-source"><pre>; clojure/contrib/template.clj:152
(defmacro template
  [&amp; form]
  (when-not (template? form)
    (throw (IllegalArgumentException. (str (pr-str form) &quot; is not a valid template.&quot;))))
  (let [form (postwalk-replace {'_ '_1} form)
        holes (find-holes form)
        pures (find-pure-exprs form)
        smap (zipmap pures (repeatedly #(gensym &quot;HOLE_&quot;)))
        newform (prewalk-replace smap form)
        ;; Now, make sure we omit nested sub-expressions:
        used (set (filter #(.startsWith (name %) &quot;HOLE_&quot;)
                          (find-symbols newform)))
        newmap (reduce (fn [m [k v]] (if (used v) (assoc m k v) m))
                       {} smap)]
    `(let ~(flatten-map (clojure.set/map-invert newmap))
       (fn ~(vec holes)
           ~@newform))))</pre></div><div class="license"><p>Copyright (c) Rich Hickey. All rights reserved.</p><p>The use and distribution terms for this software are covered by the <a href="http://opensource.org/licenses/eclipse-1.0.php">Eclipse Public License 1.0</a>, which can be found in the file
          epl-v10.html at the root of this distribution. By using this software
          in any fashion, you are agreeing to be bound by the terms of this
          license. You must not remove this notice, or any other, from this
          software.</p></div></div>